package com.nuanshui.framework.lang;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期时间工具类
 * Calendar的月份是从0开始的
 */
public class DateUtil {
	
	public static String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";

	public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

	public static String YYYY_MM_DD = "yyyy-MM-dd";
	
	public static final String[] week = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
	
	public static final String[] weekStr = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
	
	public static final String[] simpleWeekStr = { "日", "一", "二", "三", "四", "五", "六" };

	/**
	 * 获得默认的 date pattern
	 */
	public static String getDatePattern() {
		return YYYY_MM_DD_HH_MM_SS;
	}

	/**
	 * 返回预设Format的当前日期字符串
	 */
	public static String getToday() {

		final Date today = new Date();
		return format(today);
	}

	/**
	 * 根据传入的模式参数返回当天的日期
	 * 
	 * @param pattern
	 *            传入的模式
	 * @return 按传入的模式返回一个字符串
	 */
	public static String getToday(final String pattern) {
		if (!SysStringUtils.isNotEmpty(pattern)) {
			return getToday();
		}
		final Date date = new Date();
		return format(date, pattern);
	}
	
	
	public static String getNextday(final String pattern) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, 1);
        return format(calendar.getTime(), pattern);
	}

	/**
	 * 使用预设Format格式化Date成字符串
	 */
	public static String format(final Date date) {
		return date == null ? "" : format(date, getDatePattern());
	}

	/**
	 * 使用参数Format格式化Date成字符串
	 */
	public static String format(final Date date, final String pattern) {
		return date == null ? "" : new SimpleDateFormat(pattern).format(date);
	}
	
	public static String format(String dateStr,String sourcePattern,String targetPattern){
		try {
			SimpleDateFormat sf = new SimpleDateFormat(sourcePattern);
			Date date = sf.parse(dateStr);
			sf = new SimpleDateFormat(targetPattern);
			return sf.format(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return "";
		
		
	}

	/**
	 * 使用预设格式将字符串转为Date
	 */
	public static Date parse(final String strDate) {
		return !SysStringUtils.isNotEmpty(strDate) ? null : parse(strDate,
				getDatePattern());
	}

	/**
	 * 使用参数Format将字符串转为Date
	 */
	public static Date parse(final String strDate, final String pattern) {
		try {
			return !SysStringUtils.isNotEmpty(strDate) ? null
					: new SimpleDateFormat(pattern).parse(strDate);
		} catch (final ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Date addDays(final Date date, final int n) {
		final Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH, n);
		return cal.getTime();
	}
	
	/**
	 * 在日期上增加数个小时
	 */
	public static Date addHours(final Date date, final int n) {
		final Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.HOUR, n);
		return cal.getTime();
	}

	/**
	 * 在日期上增加数分钟
	 */
	public static Date addMinutes(final Date date, final int n) {
		final Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MINUTE, n);
		return cal.getTime();
	}

	/**
	 * 获取某时间的中文星期（如：星期一、星期二），每星期的第一天是星期日
	 * 
	 * @param date
	 *            ：日期
	 * @return
	 */
	public static String getWeekCS(final Date date) {
		final Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return weekStr[calendar.get(Calendar.DAY_OF_WEEK) - 1];
	}
	
	/**
	 * 获取某时间的中文星期（如：星期一、星期二），每星期的第一天是星期日
	 *
	 * @return
	 */
	public static String getWeekCS(String dateStr, String pattern){
		return getWeekCS(parse(dateStr, pattern));
	}
	
	/**
	 * 获取weekDay的简化  一 二 三 四之类的
	 * @author Gonzo
	 * @date 2017年3月6日下午11:06:50
	 * @return
	 */
	public static String getSimpleWeekDayStr(Date day){
		Calendar cal = Calendar.getInstance();
		cal.setTime(day);
		return simpleWeekStr[cal.get(Calendar.DAY_OF_WEEK) - 1];
	}
	
	/**
	 * 获取weekDay的简化  一 二 三 四之类的
	 * @author Gonzo
	 * @date 2017年3月6日下午11:06:50
	 * @return
	 */
	public static String getSimpleWeekDayStr(int weekDay){
		return simpleWeekStr[weekDay];
	}
	
	/**
	 * 获取weekDay的简化  一 二 三 四之类的
	 * @author Gonzo
	 * @date 2017年3月6日下午11:06:50
	 * @param dayStr
	 * @param pattern
	 * @return
	 */
	public static String getSimpleWeekDayStr(String dayStr, String pattern){
		return getSimpleWeekDayStr(parse(dayStr, pattern));
	}
	
	/**
	 *  获取某dayOfWeek的中文星期（如：星期一、星期二），每星期的第一天是星期日 dayOfWeek 从0开始
	 * @author Gonzo
	 * @date 2017年3月6日下午4:13:55
	 * @param dayOfWeek
	 * @return
	 */
	public static String getWeekCS(int dayOfWeek){
		return weekStr[dayOfWeek];
	}
	
	/**
	 * 获取某时间的中文星期（如：周一、周二），每星期的第一天是周日
	 *            ：日期
	 * @return
	 */
	public static String getWeekDayStr(String dateStr, String pattern){
		return getWeekDayStr(parse(dateStr, pattern));
	}
	
	/**
	 * 获取某时间的中文星期（如：周一、周二），每星期的第一天是周日
	 * 
	 * @param date
	 *            ：日期
	 * @return
	 */
	public static String getWeekDayStr(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return week[calendar.get(Calendar.DAY_OF_WEEK) - 1];
	}
	
	
	
	/**
	 * 获取某dayOfWeek的中文星期（如：周一、周二），每星期的第一天是周日 dayOfWeek 从0开始
	 * @author Gonzo
	 * @date 2017年3月6日下午4:11:49
	 * @param dayOfWeek
	 * @return
	 */
	public static String getWeekDayStrByDayOfWeek(int dayOfWeek){
		return week[dayOfWeek];
	}
	
	/**
	 * 获取当前星期
	 * @param @return
	 * @author guohao
	 * @date 2016年11月22日 上午10:10:56
	 * @since $
	 */
	public static int getCurrentWeek() {
		final Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.DAY_OF_WEEK)-1;		
	}
	
	
	public  static int  getDayNum(int addDayNum) {
		final Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, addDayNum);
		return calendar.get(Calendar.DAY_OF_MONTH);	
	}
	
	
	public static String getDayStr(String startDay,int dayNum,String pattern) {
		try {
			SimpleDateFormat sf = new SimpleDateFormat(pattern);
			Date date =  sf.parse(startDay);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.DAY_OF_MONTH, dayNum);
			return sf.format(calendar.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
		
		
	}

	/**
	 * 获取date前或后nDay天的日期
	 * 
	 * @param date ：开始日期
	 * @param nDay ：天数
	 * @param type：正为后nDay天的日期，否则为前nDay天的日期。只能为1或-1
	 * @return
	 */
	public static Date getDate(final Date date, final int nDay, final int type) {
		if ((type != 1) && (type != -1)) {
			throw new IllegalArgumentException("type的值只能为1或-1");
		}
		long millisecond = date.getTime(); // 从1970年1月1日00:00:00开始算起所经过的微秒数
		final long msel = nDay * 24 * 3600 * 1000l; // 获取nDay天总的毫秒数
		millisecond = millisecond + ((type > 0) ? msel : (-msel));
		final Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(millisecond);
		return calendar.getTime();
	}

	/**
	 * 将日期转化为字符串的形式
	 * 
	 * @param date
	 * @param strFormat
	 * @return
	 */
	public static String dateToString(final Date date, String strFormat) {
		if (strFormat == null) {
			strFormat = YYYY_MM_DD_HH_MM_SS;
		}
		final SimpleDateFormat format = new SimpleDateFormat(strFormat);
		if (date != null) {
			return format.format(date);
		} else {
			return null;
		}
	}
	
	public static String dateToString(final Date date) {
		final SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
		if (date != null) {
			return format.format(date);
		} else {
			return null;
		}
	}

	/**
	 * 将字符串转化为Date类型。如果该字符串无法转化为Date类型的数据，则返回null。
	 * 
	 * @param strDate
	 * @param strFormat
	 *            strDate和strFormat的格式要一样。即如果strDate="20061112"，则strFormat=
	 *            "yyyyMMdd"
	 * @return
	 */
	public static Date stringToDate(final String strDate, final String strFormat) {
		Date date = null;
		try {
			final SimpleDateFormat sdf = new SimpleDateFormat(strFormat);
			date = sdf.parse(strDate);
			if (!sdf.format(date).equals(strDate)) {
				date = null;
			}
		} catch (final Exception e) {
			date = null;
		}
		return date;
	}

	/**
	 * 获取n月之前或之后的日期
	 * 
	 * @param date
	 * @param nMonth
	 * @param type (只能为-1或1)
	 * @return
	 */
	public static Date addDateMonth(final Date date, final int nMonth,
			final int type) {
		if ((type != 1) && (type != -1)) {
			throw new IllegalArgumentException("type的值只能为1或-1");
		}
		final Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		final int nYear = nMonth / 12;
		final int month = nMonth % 12;
		calendar.add(Calendar.YEAR, nYear * type);
		final Date desDate = calendar.getTime();
		calendar.add(Calendar.MONTH, month * type);
		if (type < 0) {
			while (!calendar.getTime().before(desDate)) {
				calendar.add(Calendar.YEAR, type);
			}
		} else {
			while (!calendar.getTime().after(desDate)) {
				calendar.add(Calendar.YEAR, type);
			}
		}
		return calendar.getTime();
	}


	/**
	 * 创建日期date
	 * 
	 * @param year
	 *            ：年
	 * @param month
	 *            ：月
	 * @param day
	 *            ：日
	 * @return
	 */
	public static Date createDate(final int year, final int month, final int day) {
		if((1>month)||(month>=12) || (day<1) || (day >31)){
			throw new IllegalArgumentException("month的值在1和12之间，day的值在1和31之间");
		}
		final Calendar calendar = Calendar.getInstance();
		calendar.set(year, month-1, day);
		return calendar.getTime();
	}
	
	
	public static Date getDateTime(String timestr,String pattern) {
		 try {
			SimpleDateFormat format = new SimpleDateFormat(pattern);
			return format.parse(timestr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		 
		return null;
	}

	public static Date getDateByPattern(final Date date, final String pattern) {
		final SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD);
		Date curDate = null;
		try {
			curDate = format.parse(format.format(date));
		} catch (final Exception e) {
			e.printStackTrace();
		}
		return curDate;
	}
	
	public static Date getDateByPattern(final String dateStr, final String pattern) {
		final SimpleDateFormat format = new SimpleDateFormat(pattern);
		Date curDate = null;
		try {
			curDate = format.parse(dateStr);
		} catch (final Exception e) {
			e.printStackTrace();
		}
		return curDate;
	}

	public static Date getBeginDate(final Date date) {
		final Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}

	public static Date getToDate(final Date date) {
		final Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		return calendar.getTime();
	}

	public static Date stringToDate(String time) {
		SimpleDateFormat formatter;
		int tempPos = time.indexOf("AD");
		time = time.trim();
		formatter = new SimpleDateFormat(YYYY_MM_DD);
		if (tempPos > -1) {
			time = time.substring(0, tempPos) + "公元"
					+ time.substring(tempPos + "AD".length());// china
			formatter = new SimpleDateFormat("yyyy.MM.dd G 'at' hh:mm:ss z");
		}
		tempPos = time.indexOf("-");
		if ((time.indexOf("年") > -1)) {
			if (time.split(":").length == 3) {
				formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
			} else if (time.split(":").length == 2) {
				formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
			} else {
				formatter = new SimpleDateFormat("yyyy年MM月dd日");
			}
		} else if (((time.indexOf("/") > -1) && (time.indexOf("am") > -1))
				|| (time.indexOf("pm") > -1)
				|| ((time.indexOf("AM") > -1) || (time.indexOf("PM") > -1))) {
			formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
			final ParsePosition pos = new ParsePosition(0);
			final Date ctime = formatter.parse(time, pos);
			if (time.indexOf("PM") > -1) {
				ctime.setTime((ctime.getTime() + (12 * 60 * 60 * 1000)));
			}
			return ctime;

		} else if ((time.indexOf("/") > -1) && (time.indexOf(" ") > -1)) {
			if (time.split(":").length == 3) {
				formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			} else if (time.split(":").length == 2) {
				formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm");
			} else {
				formatter = new SimpleDateFormat("yyyy/MM/dd");
			}
		} else if ((time.indexOf("-") > -1) && (time.indexOf(" ") > -1)) {
			if (time.split(":").length == 3) {
				formatter = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
			} else if (time.split(":").length == 2) {
				formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			} else {
				formatter = new SimpleDateFormat(YYYY_MM_DD);
			}
		}
		final ParsePosition pos = new ParsePosition(0);
		final Date ctime = formatter.parse(time, pos);

		return ctime;
	}

	public static Timestamp getSqlTimestamp(final Date dateValue) {
		try {
			if ((dateValue == null) || ("".equals(dateValue))) {
				return null;
			}
			final Timestamp newDate = new Timestamp(dateValue.getTime());
			return newDate;
		} catch (final Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}
	
	/**
     * 以友好的方式显示时间
     * @param time
     * @return
     */
    public static String friendlyTime(final Date time) {
        //获取time距离当前的秒数
        final int ct = (int)((System.currentTimeMillis() - time.getTime())/1000);
        if(ct == 0){
            return "刚刚";
        }
        if((ct > 0) && (ct < 60)) {
            return ct + "秒前";
        }
        if((ct >= 60) && (ct < 3600)){
            return Math.max(ct / 60,1) + "分钟前";
        }
        if((ct >= 3600) && (ct < 86400)){
			return (ct / 3600) + "小时前";
		}
        if((ct >= 86400) && (ct < 2592000)){ //86400 * 30
            final int day = ct / 86400 ;           
            return day + "天前";
        }
        if((ct >= 2592000) && (ct < 31104000)) { //86400 * 30
            return (ct / 2592000) + "月前";
        }
        return (ct / 31104000) + "年前";
    }
    
    /**
     * 得到昨天的开始时间和结束时间
     * @return 返回一个数组，第一个是开始时间，第二个是结束时间
     */
    public static Date[] getYesterdayStartTimeAndEndTime(){
    	try {
    		final Date[] arrData = new Date[2];
			final Calendar ca=Calendar.getInstance();
			ca.add(Calendar.DATE, -1);
			final SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
			final SimpleDateFormat sdf2 = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
			final Date startDate = sdf2.parse(sdf.format(ca.getTime()) + " 00:00:00");
			final Date endDate = sdf2.parse(sdf.format(ca.getTime()) + " 23:59:59");
			arrData[0] = startDate;
			arrData[1] = endDate;
			return arrData;
		} catch (final ParseException e) {
			e.printStackTrace();
		}
    	return null;
    }
    
    /**
     * 得到昨天的开始时间和结束时间，以字符串形式返回
     * @return 返回一个数组，第一个是开始时间，第二个是结束时间
     */
    public static String[] getYesterdayStartTimeAndEndTimeToString(){
		final String[] arrData = new String[2];
		final Calendar ca=Calendar.getInstance();
		ca.add(Calendar.DATE, -1);
		final SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
		arrData[0] = sdf.format(ca.getTime()) + " 00:00:00";
		arrData[1] = sdf.format(ca.getTime()) + " 23:59:59";
		return arrData;
    }
    
    /**
     * 获取某天是周几   数字从0开始，星期从周日开始。 所以周日是0
     * @param @param dateStr
     * @param @param pattern
     * @author guohao
     * @date 2016年12月29日 上午9:46:12
     * @since $
     */
    public static int getWeekDay(String dateStr,String pattern) {
    	  SimpleDateFormat sf = new SimpleDateFormat(pattern);
    	  
    	  try {
			Date date =  sf.parse(dateStr);
			return getWeekDay(date);
		  } catch (ParseException e) {
				e.printStackTrace();
		  }
    	  
    	  return -1;
    	
    	
    }
    
    /**
     * 获取某天是周几  数字从0开始，星期从周日开始。 所以周日是0
     * @author Gonzo
     * @date 2017年2月16日上午11:20:11
     * @param date
     * @return
     */
    public static int getWeekDay(Date date){
    	Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(date.getTime());
		return calendar.get(Calendar.DAY_OF_WEEK)-1;
    }
    
    /**
     * 得到本月的开始时间和结束时间
     * @return 返回一个数组，第一个是开始时间，第二个是结束时间
     */
    public static Date[] getMonthStartTimeAndEndTime(){
    	try {
			final Date[] arrData = new Date[2];
			final Calendar calendar = Calendar.getInstance();     
			calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));     
			final SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
			final SimpleDateFormat sdf2 = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
			final Date startDate = sdf2.parse(sdf.format(calendar.getTime()) + " 00:00:00");
			calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));  
			final Date endDate = sdf2.parse(sdf.format(calendar.getTime()) + " 23:59:59");
			arrData[0] = startDate;
			arrData[1] = endDate;
			return arrData;
    	} catch (final ParseException e) {
			e.printStackTrace();
		}
    	return null;
    }
    
    /**
     * 根据两个日期获得这两个日期相差几天几时几分
     * @param startDate
     * @param endDate
     * @return
     */
    final static long nd = 1000*24*60*60;//一天的毫秒数
    
    public static String dateDiff(final Date startDate, final Date endDate) {
    	final long nh = 1000*60*60;//一小时的毫秒数
    	final long nm = 1000*60;//一分钟的毫秒数
    	final Calendar calender = Calendar.getInstance();
    	calender.setTime(startDate);
    	final Long millis = calender.getTimeInMillis();
    	calender.setTime(endDate);
    	final Long millis2 = calender.getTimeInMillis();
    	final Long diff = millis2 - millis;  //获得两个时间的毫秒时间差异
    	final long day = diff/nd;//计算差多少天
    	final long hour = (diff%nd)/nh;//计算差多少小时
    	final long min = (diff%nd%nh)/nm;//计算差多少分钟
    	return day+"天"+hour+"小时"+min+"分钟";
    }
    
    /**
     * 计算投放还剩几天
     * @param startDate   投放的时间
     * @param today  今天日期
     * @param day    要投放的天数
     * @return
     */
    public static Long dayNum(final Date startDate,final Date today,final int day) {
    	final Calendar calender = Calendar.getInstance();
    	calender.setTime(startDate);
    	final Long millis = calender.getTimeInMillis() + (day*nd);
    	calender.setTime(today);
    	final Long millis2 = calender.getTimeInMillis();
    	if((millis - millis2)>0){
			final Long diff = millis - millis2;
			final Long dayNum = diff/nd;  //计算差多少天
			return dayNum;
		}else {
			final Long diff = millis - millis2;
			final Long dayNum =-(diff/nd);  //计算差多少天
			return dayNum;
		}

    }
    
    /**
     * 得到某年某个月的天数
     * @param year    年份
     * @param month   月份
     * @return
     */
   public static int getMonthDayNum(final int year, final int month) {
	    final Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month-1);
		return cal.getActualMaximum(Calendar.DATE);
   }
    
   /**
    * 测试日期是否在两个日期之间
    * @author Junyan2015
    * @date 2016年1月26日 下午3:23:59
    * @param currentDate 被测日期
    * @param previousDate 
    * @param laterDate
    * @return
    */
   public static boolean isDateBetweenTwoDates(final Date currentDate, final Date previousDate, final Date laterDate) {
	   return previousDate.before(currentDate) && laterDate.after(currentDate);
   }
   
   /**
    * 获取给定的两个月之间的月数差 前闭后开的原则。
    * @author Gonzo
    * @date 2017年2月16日上午10:32:31
    * @return
    */
   public static int getMonthCountByGiveTwoDate(Date one, Date two){
	   Date temp;
	   if(one.getTime() < two.getTime()){ //使得 one 为大  two 为小
		   temp = one;
		   one = two;
		   two = temp;
	   }
	   
	   String oneDateStr =  format(one, "yyyyMM");
	   String twoDateStr =  format(two, "yyyyMM");
	   String oneDateYear = oneDateStr.substring(0, 4);
	   String twoDateYear = twoDateStr.substring(0, 4);
	   int oneDateYearInt = Integer.parseInt(oneDateYear);
	   int twoDateYearInt = Integer.parseInt(twoDateYear);
	   
	   String ontDateMonth = oneDateStr.substring(4, 6);
	   String twoDateMonth = twoDateStr.substring(4, 6);
	   
	   int oneDateMonthInt = Integer.parseInt(ontDateMonth);
	   int twoDateMonthInt = Integer.parseInt(twoDateMonth);
	   int month ;
	   if(oneDateYearInt > twoDateYearInt){
		   month = (oneDateYearInt - twoDateYearInt -1) * 12;  //-1 是因为如果是相差一年的话，是不用计算整年的月份的。
		   month += 12 - twoDateMonthInt;
		   month += oneDateMonthInt;
		   
	   }else{
		   month = oneDateMonthInt - twoDateMonthInt;
	   }
	   
	   return month;
   }
   
   /**
    *  获取给定的两个月之间的月数差 前闭后开的原则。
    * @author Gonzo
    * @date 2017年2月16日上午10:55:01
    * @param oneDateStr
    * @param ontDatePattern
    * @param twoDateStr
    * @param twoDatePattern
    * @return
    */
   public static int getMonthCountByGiveTwoDateStr(String oneDateStr, String ontDatePattern, String twoDateStr, String twoDatePattern){
	  return getMonthCountByGiveTwoDate(parse(oneDateStr, ontDatePattern), parse(twoDateStr, twoDatePattern));
   }
   
   
   /**
    * 获取给定时间所在月份的最大天
    * @author Gonzo
    * @date 2017年2月16日下午2:42:34
    * @return
    */
   public static Date getMaxDateByGiveDate(Date date){
	   Calendar calendar = Calendar.getInstance();
	   calendar.setTime(date);
	   calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
	   return calendar.getTime();
   }
   
   /**
    * 获取给定时间所在月份的最大天
    * @author Gonzo
    * @date 2017年2月16日下午2:42:34
    * @param dateStr
    * @param datePattern
    * @return
    */
   public static Date getMaxDateByGiveDateStr(String dateStr, String datePattern){
	  return  getMaxDateByGiveDate(parse(dateStr, datePattern));
   }
   
   /**
    * 获取给定时间所在月份的最小天
    * @author Gonzo
    * @date 2017年2月16日下午2:41:57
    * @return
    */
   public static Date getMixDateByGiveDate(Date date){
	   Calendar calendar = Calendar.getInstance();
	   calendar.setTime(date);
	   calendar.set(Calendar.DAY_OF_MONTH, 1);
	   return calendar.getTime();
   }
   
   /**
    * 获取给定时间所在月份的最小天
    * <b>Calendar 周从周日开始，周日是1，但是项目里面周从0开始</b>
    * @author Gonzo
    * @date 2017年2月16日下午2:41:57
    * @param dateStr
    * @param datePattern
    * @return
    */
   public static Date getMixDateByGiveDateStr(String dateStr, String datePattern){
	  return getMixDateByGiveDate(parse(dateStr, datePattern));
   }
   
   /**
    * 获取给定时间的周日的的那一天
    * @author Gonzo
    * @date 2017年2月23日下午2:22:02
    * @param day
    * @return
    */
   public static Date getFirstWeekDay(Date day){
	   Calendar calendar = Calendar.getInstance();
	   calendar.setTime(day);
	   int weekDay = calendar.get(Calendar.DAY_OF_WEEK);//周从周日开始，周日是1
	   calendar.add(Calendar.DATE, (weekDay - Calendar.SUNDAY) * -1);
	   return calendar.getTime();
   }
   
   /**
    * 获取给定时间的周日的的那一天
    * @author Gonzo
    * @date 2017年2月23日下午2:22:34
    * @param dayStr
    * @param datePattern
    * @return
    */
   public static Date getFirstWeekDay(String dayStr, String datePattern){
	   return getFirstWeekDay(parse(dayStr, datePattern));
   }
   
   /**
    * 将一个 0900格式的时间加上minute  然后返回
    * @auth Gonzo
    * 2017年3月28日下午8:27:59
    * @param minute
    * @param startTime
    * @return
    */
   public static String addMinute( String startTime, Integer minute){
	   Calendar calendar = Calendar.getInstance();
	   
		String dateTempStr = DateUtil.format(new Date(), "yyyyMMdd");
		dateTempStr = dateTempStr + startTime;
		Date dateTemp = DateUtil.parse(dateTempStr, "yyyyMMddHHmm");
		
		calendar.setTime(dateTemp);
		calendar.add(Calendar.MINUTE, minute);
		dateTemp = calendar.getTime();
		
		return DateUtil.format(dateTemp, "HHmm");
   }
   
   /**
    * 获取两个时间相差的天数
    * @author Gonzo
    * @date 2017年2月28日下午4:25:25
    * @return
    */
   public static long getDifferenceDayByTwoDate(Date dateOne, Date dateTwo){
	   long maxTime = dateOne.getTime();
	   long mixTime = dateTwo.getTime();
	   
	   if(maxTime < mixTime){
		   maxTime = maxTime + mixTime;
		   mixTime = maxTime - mixTime;
		   maxTime = maxTime - mixTime;
	   }
	   return (maxTime - mixTime)  / (1000 * 60 * 60 * 24);
	   
   }
   
   /**
    * 获取两个时间相差的天数
    * @author Gonzo
    * @date 2017年2月28日下午4:25:25
    * @param dateOneStr
    * @param dateOneStrPatten
    * @param dateTwoStr
    * @param dateTwoStrPatten
    * @return
    */
   public static long getDifferenceDayByTwoDate(String dateOneStr, String dateOneStrPatten, String dateTwoStr, String dateTwoStrPatten){
	   return getDifferenceDayByTwoDate(parse(dateOneStr, dateOneStrPatten), parse(dateTwoStr, dateTwoStrPatten));
   }

	/**
	 * 获取两个日期之间所差的天数
	 *
	 * @param from：开始日期
	 * @param to：结束日期
	 * @return：所差的天数(非负整数)
	 */
	public static int dateNum(final Date from, final Date to) {
		final Calendar calendar = Calendar.getInstance();
		calendar.setTime(from);
		calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		final Date fromDate = calendar.getTime();
		System.out.println("开始日期"+DateUtil.format(fromDate,DateUtil.DEFAULT_DATE_PATTERN));

		calendar.setTime(to);
		calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		final Date toDate = calendar.getTime();
		System.out.println("结束日期"+DateUtil.format(toDate,DateUtil.DEFAULT_DATE_PATTERN));
		final int diff = Math
				.abs((int) ((fromDate.getTime() - toDate.getTime()) / (24 * 3600 * 1000)));
		System.out.println(fromDate.getTime());
		System.out.println(toDate.getTime());
		return diff;
	}
	/*
	 * 两个日期相差天数
	 * @param from：开始日期
	 * @param to：结束日期
	 * @return：所差的天数(非负整数)
	 */
	public static long getIntervalDay(Date smdate,Date bdate) {
		long between_days=0;
		try{
			SimpleDateFormat sdf=new SimpleDateFormat(DateUtil.DEFAULT_DATE_PATTERN);
			smdate=sdf.parse(sdf.format(smdate));
			bdate=sdf.parse(sdf.format(bdate));
			Calendar cal = Calendar.getInstance();
			cal.setTime(smdate);
			long time1 = cal.getTimeInMillis();
			cal.setTime(bdate);
			long time2 = cal.getTimeInMillis();
			 between_days=Math.abs((time2-time1)/(1000*3600*24));
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			return between_days;
		}
	}

	/**
	 * 获取某个具体的需要的时分秒
	 * @author Wxw
	 * @date 2017年7月25日下午4:25:25
	 * @param date
	 * @param hour
	 * @param minute
	 * @param second
	 * @return
	 */
	public static Date getDateHourMinuteSecond(Date date,int hour,int minute,int second){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.SECOND, second);
		calendar.set(Calendar.MILLISECOND,0);
		return calendar.getTime();
	}
   
   public static void main(String[] args) throws Exception{
		for(int i=0;i<100;i++){
			System.out.println(getDateHourMinuteSecond(new Date(),23,59,58));
			Thread.sleep(500);
		}

	}
	
   
}